package com.liangwj.springbootdesignpatterns.structureTypePatterns.filter;

import java.util.List;

/**
 * <b>Description:</b> Or Criteria的接口实现的实体类 <br/>
 * @author Lingwenjun
 * @date 2019/1/24-17:04
 * @version 1.0
 */
public class OrCriteria implements Criteria{

    private Criteria criteria;

    private Criteria otherCriteria;

    public OrCriteria(Criteria criteria, Criteria otherCriteria) {
        this.criteria = criteria;
        this.otherCriteria = otherCriteria;
    }

    @Override
    public List<Person> meetCriteria(List<Person> persons) {
        List<Person> firstCriteriaItems = criteria.meetCriteria(persons);
        List<Person> otherCriteriaItems = otherCriteria.meetCriteria(persons);

        //把2种或条件符合的 合并到第一个条件中去
        for (Person person : otherCriteriaItems) {
            if (!firstCriteriaItems.contains(person)) {
                firstCriteriaItems.add(person);
            }
        }
        return firstCriteriaItems;
    }
}
